package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.fragment.ClassifyFragment;
import com.example.lanouhn.firstapp.activity.fragment.HomePageFragment;
import com.example.lanouhn.firstapp.activity.fragment.MineFragment;
import com.example.lanouhn.firstapp.activity.fragment.ShoppingCarFragment;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends FragmentActivity {

    //定义Button
    private RadioGroup radioGroup;

    private String key;
    //定义Fragment布局变量
    private HomePageFragment homePageFragment;
    private ClassifyFragment classifyFragment;
    private ShoppingCarFragment shoppingCarFragment;
    private MineFragment mineFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        key = (String) SPUtils.get(MainActivity.this, "log_key", "");

        //找控件
        initView();
        //initData动态加载Fragment
        initData();
        //点击事件
        setOnClickListener();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //需要再进行一次赋值   配合下边的判断  使得登陆以后key值不为空
        key = (String) SPUtils.get(MainActivity.this, "log_key", "");
        if (requestCode == 100 && resultCode == 100) {
            // 购物车页
            //加载布局管理器
            FragmentManager manager2 = getSupportFragmentManager();
            //开启一个事务
            final FragmentTransaction transaction2 = manager2.beginTransaction();
            transaction2.replace(R.id.frameLayout, shoppingCarFragment);
            // 当按返回键的时候 会回到替换之前的fragment
            transaction2.addToBackStack(null);
            transaction2.commit();
        } else if (requestCode == 101 && resultCode == 100) {
            //个人页
            //加载布局管理器
            FragmentManager manager3 = getSupportFragmentManager();
            //开启一个事务
            final FragmentTransaction transaction3 = manager3.beginTransaction();
            transaction3.replace(R.id.frameLayout, mineFragment);
            // 当按返回键的时候 会回到替换之前的fragment
            transaction3.addToBackStack(null);
            transaction3.commit();
        } else if (resultCode == 105) {
            //加载布局管理器
            FragmentManager manager = getSupportFragmentManager();
            //开启一个事务
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.frameLayout, homePageFragment).commit();

            changeRadioButtonBg(R.id.rb_homePager);
        }
    }

    

    /**


     * 点击加载  切换布局
     */
    private void setOnClickListener() {
        //加载布局管理器
        FragmentManager manager = getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction transaction = manager.beginTransaction();
        //指定添加的Fragment布局   并且提交事务
        transaction.add(R.id.frameLayout, homePageFragment).commit();

        changeRadioButtonBg(R.id.rb_homePager);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {


                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_homePager:
                        // 设置当前的ViewPager的item为第一个

                        changeRadioButtonBg(R.id.rb_homePager);
                        //加载布局管理器
                        FragmentManager manager = getSupportFragmentManager();
                        //开启一个事务
                        FragmentTransaction transaction = manager.beginTransaction();
                        transaction.replace(R.id.frameLayout, homePageFragment).commit();
                        break;
                    case R.id.rb_classify:
                        // 切换分类页
                        //加载布局管理器
                        FragmentManager manager1 = getSupportFragmentManager();
                        //开启一个事务
                        final FragmentTransaction transaction1 = manager1.beginTransaction();
                        transaction1.replace(R.id.frameLayout, classifyFragment);
                        // 当按返回键的时候 会回到替换之前的fragment
                        transaction1.addToBackStack(null);
                        transaction1.commit();
                        changeRadioButtonBg(R.id.rb_classify);
                        break;
                    case R.id.rb_shoppingCar:


                        changeRadioButtonBg(R.id.rb_shoppingCar);
                        //判断是否  用户是否登录 已登录的话直接切换Fragment   未登录的话进行跳转到登录页面
                        if (key.equals("")) {
                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this, LoginActivity.class);
                            startActivityForResult(intent, 100);

                        } else {
                            // 购物车页
                            //加载布局管理器
                            FragmentManager manager2 = getSupportFragmentManager();
                            //开启一个事务
                            final FragmentTransaction transaction2 = manager2.beginTransaction();
                            transaction2.replace(R.id.frameLayout, shoppingCarFragment);
                            // 当按返回键的时候 会回到替换之前的fragment
                            transaction2.addToBackStack(null);
                            transaction2.commit();

                        }
                        break;
                    case R.id.rb_personCenter:
                        changeRadioButtonBg(R.id.rb_personCenter);
                        //判断是否  用户是否登录 已登录的话直接切换Fragment   未登录的话进行跳转到登录页面
                        if (key.equals("")) {
                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this, LoginActivity.class);
                            startActivityForResult(intent, 101);
                        } else {
                            //个人页
                            //加载布局管理器
                            FragmentManager manager3 = getSupportFragmentManager();
                            //开启一个事务
                            final FragmentTransaction transaction3 = manager3.beginTransaction();
                            transaction3.replace(R.id.frameLayout, mineFragment);
                            // 当按返回键的时候 会回到替换之前的fragment
                            transaction3.addToBackStack(null);
                            transaction3.commit();
                        }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 创建方法来接收负页面传递过来的“symbol”，判断然后显示不同的Fragment
         */


        int id = getIntent().getIntExtra("symbol", 0);
        if (id > 0)
            switch (id) {
                case 1:
                    //加载布局管理器
                    FragmentManager manager = getSupportFragmentManager();
                    //开启一个事务
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.replace(R.id.frameLayout, homePageFragment).commit();
                    setVisible(true);
                    changeRadioButtonBg(R.id.rb_homePager);
                    break;
                case 2:
                    FragmentManager manager1 = getSupportFragmentManager();
                    //开启一个事务
                    final FragmentTransaction transaction1 = manager1.beginTransaction();
                    transaction1.replace(R.id.frameLayout, classifyFragment);
                    // 当按返回键的时候 会回到替换之前的fragment
                    transaction1.addToBackStack(null);
                    transaction1.commit();
                    setVisible(true);
                    changeRadioButtonBg(R.id.rb_classify);
                    break;
                case 3:
                    //判断是否  用户是否登录 已登录的话直接切换Fragment   未登录的话进行跳转到登录页面
                    if (key == "") {
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        FragmentManager manager2 = getSupportFragmentManager();
                        //开启一个事务
                        final FragmentTransaction transaction2 = manager2.beginTransaction();
                        transaction2.replace(R.id.frameLayout, shoppingCarFragment);
                        // 当按返回键的时候 会回到替换之前的fragment
                        transaction2.addToBackStack(null);
                        transaction2.commit();
                        setVisible(true);
                        changeRadioButtonBg(R.id.rb_shoppingCar);
                    }
                    break;
                case 4:
                    //判断是否  用户是否登录 已登录的话直接切换Fragment   未登录的话进行跳转到登录页面
                    if (key == "") {
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);

                    } else {
                        FragmentManager manager3 = getSupportFragmentManager();
                        //开启一个事务
                        final FragmentTransaction transaction3 = manager3.beginTransaction();
                        transaction3.replace(R.id.frameLayout, mineFragment);
                        // 当按返回键的时候 会回到替换之前的fragment
                        transaction3.addToBackStack(null);
                        transaction3.commit();
                        changeRadioButtonBg(R.id.rb_personCenter);
                        break;

                    }
            }

    }
    private void initData() {
        //定义初始页面
        homePageFragment = new HomePageFragment();
        classifyFragment = new ClassifyFragment();
        shoppingCarFragment = new ShoppingCarFragment();
        mineFragment = new MineFragment();
    }

    /**
     * 找控件
     */
    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

    }


    private void changeRadioButtonBg(int id) {

        Drawable press_one = getResources().getDrawable(R.mipmap.press_one);
        Drawable one = getResources().getDrawable(R.mipmap.one);
        Drawable press_classify = getResources().getDrawable(R.mipmap.press_classify);
        Drawable classify = getResources().getDrawable(R.mipmap.classify);
        Drawable press_shopping_cart = getResources().getDrawable(R.mipmap.press_shopping_cart);
        Drawable shopping_cart = getResources().getDrawable(R.mipmap.shopping_cart);
        Drawable press_person_center = getResources().getDrawable(R.mipmap.press_person_center);
        Drawable person_center = getResources().getDrawable(R.mipmap.person_center);
        //调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        press_one.setBounds(0, 0, press_one.getMinimumWidth(), press_one.getMinimumHeight());
        one.setBounds(0, 0, one.getMinimumWidth(), one.getMinimumHeight());
        press_classify.setBounds(0, 0, press_classify.getMinimumWidth(), press_classify.getMinimumHeight());
        classify.setBounds(0, 0, classify.getMinimumWidth(), classify.getMinimumHeight());
        press_shopping_cart.setBounds(0, 0, press_shopping_cart.getMinimumWidth(), press_shopping_cart.getMinimumHeight());
        shopping_cart.setBounds(0, 0, shopping_cart.getMinimumWidth(), shopping_cart.getMinimumHeight());
        press_person_center.setBounds(0, 0, press_person_center.getMinimumWidth(), press_person_center.getMinimumHeight());
        person_center.setBounds(0, 0, person_center.getMinimumWidth(), person_center.getMinimumHeight());

        switch (id) {

            case R.id.rb_homePager:
                ((RadioButton) findViewById(R.id.rb_homePager)).setCompoundDrawables(null, press_one, null, null); //设置图标
                ((RadioButton) findViewById(R.id.rb_classify)).setCompoundDrawables(null, classify, null, null);
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setCompoundDrawables(null, shopping_cart, null, null);
                ((RadioButton) findViewById(R.id.rb_personCenter)).setCompoundDrawables(null, person_center, null, null);

                ((RadioButton) findViewById(R.id.rb_homePager)).setTextColor(getResources().getColor(R.color.radioButtonChecked));
                ((RadioButton) findViewById(R.id.rb_classify)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_personCenter)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                break;
            case R.id.rb_classify:
                ((RadioButton) findViewById(R.id.rb_homePager)).setCompoundDrawables(null, one, null, null); //设置图标
                ((RadioButton) findViewById(R.id.rb_classify)).setCompoundDrawables(null, press_classify, null, null);
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setCompoundDrawables(null, shopping_cart, null, null);
                ((RadioButton) findViewById(R.id.rb_personCenter)).setCompoundDrawables(null, person_center, null, null);

                ((RadioButton) findViewById(R.id.rb_homePager)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_classify)).setTextColor(getResources().getColor(R.color.radioButtonChecked));
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_personCenter)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));

                break;
            case R.id.rb_shoppingCar:
                ((RadioButton) findViewById(R.id.rb_homePager)).setCompoundDrawables(null, one, null, null); //设置图标
                ((RadioButton) findViewById(R.id.rb_classify)).setCompoundDrawables(null, classify, null, null);
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setCompoundDrawables(null, press_shopping_cart, null, null);
                ((RadioButton) findViewById(R.id.rb_personCenter)).setCompoundDrawables(null, person_center, null, null);

                ((RadioButton) findViewById(R.id.rb_homePager)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_classify)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setTextColor(getResources().getColor(R.color.radioButtonChecked));
                ((RadioButton) findViewById(R.id.rb_personCenter)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                break;
            case R.id.rb_personCenter:
                ((RadioButton) findViewById(R.id.rb_homePager)).setCompoundDrawables(null, one, null, null); //设置图标
                ((RadioButton) findViewById(R.id.rb_classify)).setCompoundDrawables(null, classify, null, null);
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setCompoundDrawables(null, shopping_cart, null, null);
                ((RadioButton) findViewById(R.id.rb_personCenter)).setCompoundDrawables(null, press_person_center, null, null);

                ((RadioButton) findViewById(R.id.rb_homePager)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_classify)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_shoppingCar)).setTextColor(getResources().getColor(R.color.radioButtonNotChecked));
                ((RadioButton) findViewById(R.id.rb_personCenter)).setTextColor(getResources().getColor(R.color.radioButtonChecked));
                break;
        }
    }


    /**
     * 点击两次退出
     */

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        exitB2Click();
        return false;
    }

    private static boolean isExit = false;

    private void exitB2Click() {

        //用来记录两次点击返回键的时间
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;//准备退出
            Toast.makeText(MainActivity.this, "再点一次退出尿了吗", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;//取消退出
                }
            }, 2000);

        } else {

            finish();
        }

    }

}
